<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="openAdd(1, '0')">
              添加权限
            </el-button>
          </el-row>
        </div>
        <!-- card body -->
        <el-table border :data="list" row-key="id">
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                type="text"
                @click="openAdd(2, row.id)"
              >
                添加
              </el-button>
              <el-button type="text" @click="addEdit(row.id)">编辑</el-button>
              <el-button type="text" @click="delDerw(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 添加表单的对话框 -->
    <el-dialog
      :visible.sync="showDialog"
      :title="formData.id ? '编辑标题' : '添加标题'"
      @close="close"
    >
      <el-form ref="rm" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="addList">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { tranformTreeData } from '@/utils'
export default {
  name: 'Permission',
  data () {
    return {
      // 渲染列表的数据
      list: [],
      // 添加表单框的显示与隐藏
      showDialog: false,
      // 添加表单框的数据
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      // 表单框的数据校验
      rules: {
        name: [{ required: true, message: '权限点名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限点标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 删除功能
    async delDerw (row) {
      try {
        await this.$confirm(`确定删除${row.name}权限吗`, '提示')
        if (row.children && row.children.length > 0) {
          return this.$message.error('不能直接删除具有子权限的权限')
        }
        await delPermission(row.id)
        this.getList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取列表数据
    async getList () {
      const res = await getPermissionList()
      this.list = tranformTreeData(res)
    },
    // 点击编辑
    async addEdit (id) {
      this.showDialog = true
      const res = await getPermissionDetail(id)
      this.formData = res
    },
    // 点击添加按钮
    openAdd (type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭对话框
    close () {
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
      this.$refs.rm.resetFields()
    },
    // 确认添加按钮
    async addList () {
      try {
        await this.$refs.rm.validate()
        if (this.formData.id) {
          await updatePermission(this.formData)
        } else {
          await addPermission(this.formData)
        }
        this.showDialog = false
        this.getList()
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
